/**
 * layui下拉扩展
 * @author HandsomeHan
 * @since 2018/12/7 11:25:22
 */
layui.define(['form'], function (exports) {
    var form = layui.form,
        $ = layui.jquery;
    var defaultOptionHtml=null;

    var select = {
        render: function (option) {
            let $obj = $(option.elem);
            let filterName =  $obj.attr('lay-filter')===undefined?'zmn-select-' + uuid():$obj.attr('lay-filter');
            $obj.attr('lay-filter', filterName);
            select.load(option);

            if (option.child != null) {
                form.on("select(" + filterName + ")", function (obj) {
                    //如果选中提示项,就在子级提示项中提示先选择上一项
                    var isTips = obj.value === 0 || obj.value === '' || obj.value === null;
                    defaultOptionHtml = defaultOptionHtml==null?$(option.child.elem + ' > option'):defaultOptionHtml;
                    if (isTips) {
                        function clear(_option) {
                           /* parentId = _option.elem.substring(1);
                            var parentLableName = $('label[for="' + parentId + '"]').text();
                            defaultOptionHtml = "<option value=''>请先选择" + parentLableName + "</option>";*/
                            $(_option.child.elem).empty().html(defaultOptionHtml).val('');
                            if (_option.child.child == null) {
                                return;
                            }
                            clear(_option.child);
                        }

                        clear(option);
                        form.render("select");
                        return;
                    }

                    option.child['value'] = obj.value;
                    select.render(option.child);

                });
            }
        },
        init: function (elem, data) {
            var optionHtml = '<option value="">请选择</option>';
            for (var i = 0; i < data.length; i++) {
                optionHtml += '<option value="' + data[i].value + '">' + data[i].text + '</option>';
            }
            $(elem).html(optionHtml);
            form.render("select");
        },
        load: function (option) {
            var isRemote = !(option.url === null || option.url === '');
            if (isRemote) {
                $.ajax({
                    type: "GET",
                    url: option.url,
                    dataType: "json",
                    data: function () {
                        if (option.value!=null|| option.value !== '') {
                            var paramObj = {};
                            if (option.param === null || option.param === '') {
                                return paramObj['param'] = option.value;
                            }
                            var parameterName = option.param;
                            paramObj[parameterName] = option.value;
                            return paramObj;
                        }
                    }(),
                    success: function (OriginalData) {
                        if (typeof (option.parseData) === 'function') {
                            var convertedData = option.parseData(OriginalData);
                            select.init(option.elem, convertedData);
                            return;
                        }
                        select.init(option.elem, OriginalData);
                    }
                });
                return;
            }
            init(option.elem, option.data);
        }
    };

    /**

     * @author HandsomeHan

     * @description 生成uuid

     * @param {int} [len]   可选,生成uuid的长度,默认36位,建议20位以上

     * @param {int} [radix]   可选,生成的进制基数，8是8进制,10是10进制等等

     * @returns {String}, 生成的UUID

     **/

    function uuid (len, radix) {

        var uuid;

        var chars, i, r, uuid;

        chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');

        uuid = [];

        i = void 0;

        radix = radix || chars.length;

        if (len) {

            i = 0;

            while (i < len) {

                uuid[i] = chars[0 | Math.random() * radix];

                i++;

            }

        } else {

            r = void 0;

            uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';

            uuid[14] = '4';

            i = 0;

            while (i < 36) {

                if (!uuid[i]) {

                    r = 0 | Math.random() * 16;

                    uuid[i] = chars[i === 19 ? r & 0x3 | 0x8 : r];

                }

                i++;

            }

        }

        return uuid.join('');

    }
    exports('select', select);
});